ig.module(
	'game.entities.playButton'
)
.requires(
	'impact.entity',
        'game.entities.base.baseButton',
	'game.entities.background',	
	'game.entities.background2',
	
	'game.entities.questionBar',
	'game.entities.linkButton',	
	'game.entities.optionButton',
	'game.entities.nextButton',
	'game.entities.previousButton',
	'game.entities.finishButton',
	'game.entities.elinf'	
)
.defines(function()
{
    EntityPlayButton = EntityBaseButton.extend(
    {
        size: {x:144, y:47},	
        normalButton: new ig.Image("media/images/start.png"),
        overButton: new ig.Image("media/images/startOver.png"),
	
	init: function(x, y, settings)
	{
	    this.parent(x, y, settings);	    
	},	
	
        onClick: function()
        {
            this.parent();
            
	    if (ig.global.accessDenied == true)
	    {
		return;
	    }	    
	    
	    var postUrl = "SubmitStartTest.aspx?testID=" + ig.global.testID + "&Id_Usuario=" + ig.global.studentID + "&Mode=" + ig.global.mode + "&cpf=" + ig.global.cpf;
	    var ocorreuErro = false;
	    $.ajax({
		type: "POST",
		url: postUrl,
		async: false,
		success: function (resposta) {
		    if (resposta != null && resposta != "" && resposta != " ")
		    {
			ig.global.accessDenied = true;
			alert("#1 Ocorreu um erro ao iniciar a prova.\nFeche a pagina e tente novamente dentro de alguns minutos.\nErro: " + resposta)
			window.location = window.location;
			ocorreuErro = true;
		    }
		},
		error: function (resposta) {
		    ig.global.accessDenied = true;
		    alert("#2 Ocorreu um erro ao iniciar a prova.\nFeche a pagina e tente novamente dentro de alguns minutos.\nErro: " + resposta)
		    window.location = window.location;
		    ocorreuErro = true;
		},
	    });
	    
	    if (ocorreuErro == false)
	    {
		ig.global.paused = false;
		ig.global.started = true;
		
		ig.global.failed = false;
		ig.global.paused = false;
		ig.global.numberOfPicks = 0;                            
		ig.global.justStarted = true;
		
		// Spawning the buttons
		var buttonX = 15;
		var buttonY = 5;
		var buttonOffsetY = 80;
		    
		ig.game.spawnEntity(EntityOptionButton, buttonX, buttonY);       
		ig.game.spawnEntity(EntityOptionButton, buttonX, buttonY += buttonOffsetY);	      
		ig.game.spawnEntity(EntityOptionButton, buttonX, buttonY += buttonOffsetY);  
		ig.game.spawnEntity(EntityOptionButton, buttonX, buttonY += buttonOffsetY);	    	    
		
		var buttons = ig.game.getEntitiesByType(EntityOptionButton); 	    
		buttons[0].text = ig.global.fullQuestion[ig.global.currentQuestion].one;
		buttons[1].text = ig.global.fullQuestion[ig.global.currentQuestion].two;
		buttons[2].text = ig.global.fullQuestion[ig.global.currentQuestion].three;
		buttons[3].text = ig.global.fullQuestion[ig.global.currentQuestion].four;
		
		buttons[0].option =	1;
		buttons[1].option =	2;
		buttons[2].option =	3;
		buttons[3].option =	4;
		
		// Spawning navigation
		buttonY += 32;
		ig.game.spawnEntity(EntityPreviousButton, 626, buttonY);
		ig.game.spawnEntity(EntityNextButton, 756, buttonY);
		ig.game.spawnEntity(EntityFinishButton, 688, buttonY - 272);		    
		
		// Spawning the elinf
		ig.game.spawnEntity(EntityElinf, 680, 42);	    
		ig.game.sortEntitiesDeferred();
		    
		$("#canvas").click(function(event)
		{
		  //alert("As you can see, the link no longer took you to jquery.com");
		});  		
	    }
        },	
    });
});